package com.guli.gulimall.product.service.impl;

import com.guli.gulimall.product.dao.BrandDao;
import com.guli.gulimall.product.dao.CategoryDao;
import com.guli.gulimall.product.entity.BrandEntity;
import com.guli.gulimall.product.entity.CategoryEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.gulimall.product.dao.CategoryBrandRelationDao;
import com.guli.gulimall.product.entity.CategoryBrandRelationEntity;
import com.guli.gulimall.product.service.CategoryBrandRelationService;
import sun.rmi.runtime.Log;


@SuppressWarnings("all")
@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private BrandDao brandDao;
    @Autowired
    private CategoryDao categoryDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryBrandRelationEntity> catelogList(Long brandId) {
        QueryWrapper<CategoryBrandRelationEntity> w = new QueryWrapper<>();
        w.eq("brand_id", brandId);
        return baseMapper.selectList(w);
    }

    @Override
    public List<CategoryBrandRelationEntity> brandsList(Long catId) {
        QueryWrapper<CategoryBrandRelationEntity> w = new QueryWrapper<>();
        w.eq("catelog_id",catId);
        List<CategoryBrandRelationEntity> categoryBrandRelationEntities = baseMapper.selectList(w);
        return categoryBrandRelationEntities;
    }

    @Override
    public boolean save(CategoryBrandRelationEntity entity) {
        Long brandId = entity.getBrandId();
        Long catelogId = entity.getCatelogId();
        BrandEntity brandEntity = brandDao.selectById(brandId);
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
        entity.setBrandName(brandEntity.getName());
        entity.setCatelogName(categoryEntity.getName());
        return super.save(entity);
    }
}